package com.steven.door.manager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.steven.door.base.Constant;
import com.steven.door.receiver.WEBReceive;
import com.steven.door.webSocket.WebSocketCallBack;
import com.steven.door.webSocket.WebSocketHandler;


public class WebSocketManager {
    private static String TAG = "webSocket";
    @SuppressLint("StaticFieldLeak")
    private static WebSocketManager webSocketManager;
    private Context mContext;
    private Intent it;
    private static MyHandler myHandler;
    private static WebSocketHandler webSocketHandler;

    private WebSocketManager(Context context) {
        this.mContext=context;
        webSocketHandler = WebSocketHandler.getInstance(Constant.wsUrl);
        it = new Intent(context, WEBReceive.class);
        myHandler = new MyHandler();
    }

    public static WebSocketManager getInstance(Context context) {
        if (webSocketManager == null) {
            synchronized (WebSocketManager.class) {
                webSocketManager = new WebSocketManager(context);
            }
        }

        return webSocketManager;
    }

    public void connect() {
        webSocketHandler.connect();
        webSocketHandler.setSocketIOCallBack(new WebSocketCallBack() {
            @Override
            public void onOpen() {//连接成功
                webSocketHandler.send("uid1");//上报唯一标识  表示设备已经注册
//                sendHeartBeat();//通过闹钟定时器 实现发送心跳包
                myHandler.sendEmptyMessageDelayed(0, 10000);//10秒发送一次
            }

            @Override
            public void onMessage(String data) {//接收到消息
//                it.putExtra("data", data);
//                mContext.sendBroadcast(it);
                Log.e(TAG, "onMessage: "+data );
//                EventBus.getDefault().post(data);
            }

            @Override
            public void onClose() {//连接断开
            }

            @Override
            public void onConnectError(Throwable t) {//连接失败 或 超时
                Log.e(TAG, "onConnectError: 连接失败!" + t);
                webSocketHandler.reConnect();
            }
        });
    }

    //定义心跳包  间隔五秒
    @SuppressLint("ShortAlarm")
    private void sendHeartBeat() {
        //开始发送心跳包
//        Intent intent = new Intent(mContext, HeartBeatReceive.class);
//        intent.setAction("repeating");
//        PendingIntent sender = PendingIntent
//                .getBroadcast(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//        //开始时间
//        long firsTime = System.currentTimeMillis();//SystemClock.elapsedRealtime()
//        AlarmManager am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);//5秒一个周期，不停的发送广播
//        assert am != null;
//        am.setRepeating(AlarmManager.RTC_WAKEUP, firsTime, Constant.heartTime, sender);
    }

    @SuppressLint("HandlerLeak")
    private static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            webSocketHandler.send("heart");
            Log.e(TAG, "handleMessage: 心跳包!");
            myHandler.sendEmptyMessageDelayed(0, 10000);
        }
    }

}
